#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from autoapi.common.log import log
from autoapi.core.get_conf import autoapi_config
from autoapi.utils.rich_console import console


class FeiShu:
    def __init__(self, content: dict):
        self.content = content

    def send(self) -> None:
        # 发送飞书消息
        try:
            import requests

            headers = {
                "Content-Type": "application/json; charset=utf-8",
                "Connection": "close",
            }
            data = {
                "msg_type": "post",
                "content": {
                    "post": {
                        "zh_cn": {
                            "title": autoapi_config.TEST_REPORT_TITLE,
                            "content": [
                                [
                                    {
                                        "tag": "text",
                                        "text": f"👤 测试人员: {autoapi_config.TESTER_NAME}",
                                    }
                                ],
                                [
                                    {
                                        "tag": "text",
                                        "text": f'🤖 测试结果: {self.content["result"]}',
                                    }
                                ],
                                [
                                    {
                                        "tag": "text",
                                        "text": f'✅ 通过用例: {self.content["passed"]}',
                                    }
                                ],
                                [
                                    {
                                        "tag": "text",
                                        "text": f'🔧 失败用例: {self.content["failed"]}',
                                    }
                                ],
                                [
                                    {
                                        "tag": "text",
                                        "text": f'❌ 错误用例: {self.content["error"]}',
                                    }
                                ],
                                [
                                    {
                                        "tag": "text",
                                        "text": f'⚠️ 跳过用例: {self.content["skipped"]}',
                                    }
                                ],
                                [
                                    {
                                        "tag": "text",
                                        "text": f'⌛ 开始时间: {self.content["started_time"]}',
                                    }
                                ],
                                [
                                    {
                                        "tag": "text",
                                        "text": f'⏱️ 执行耗时: {self.content["elapsed"]}',
                                    }
                                ],
                                [
                                    {
                                        "tag": "a",
                                        "text": "➡️ 查看详情",
                                        "href": f"{autoapi_config.JENKINS_URL}",
                                    }
                                ],
                            ],
                        }
                    }
                },
            }
            mention_user_str = getattr(autoapi_config, "FEISHU_MENTION_USERS", "")
            if isinstance(mention_user_str, str):
                mention_user_list = [
                    uid.strip() for uid in mention_user_str.split(",") if uid.strip()
                ]
            else:
                mention_user_list = mention_user_str or []

            if mention_user_list:
                at_line = [
                    {"tag": "text", "text": "📣 提醒："},
                    *[{"tag": "at", "user_id": uid} for uid in mention_user_list],
                ]
                data["content"]["post"]["zh_cn"]["content"].insert(0, at_line)
            response = requests.session().post(
                url=autoapi_config.FEISHU_WEBHOOK,
                json=data,
                headers=headers,
                proxies=autoapi_config.FEISHU_PROXY,  # type: ignore
            )
            response.raise_for_status()
        except Exception as e:
            log.error(f"🪧 飞书消息发送异常: {e}")
            console.print(f"🪧 飞书消息发送异常: {e}")
        else:
            log.success("✈️ 飞书消息发送成功")
            console.print("✈️ 飞书消息发送成功")
